package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.exception.ProjectRuntimeException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.OrgUserService;
import com.dp.appi.service.ProjectService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/4/26.
 * 项目
 */
@Controller
@RequestMapping(value = "/projectControl")
public class ProjectControl {
    private static Logger log = LoggerFactory.getLogger(ProjectControl.class);
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private OrgUserService orgUserService;

    /**
     * 个人项目列表
     *
     * @param modelMap
     * @param s        开始序列
     * @param n        获取条数
     * @param param    openIdent
     */
    @RequestMapping(value = "/projectList", method = RequestMethod.GET)
    public void projectList(ModelMap modelMap, @RequestParam Map<String, String> param, Integer s, Integer n) {
        try {
            String openIdent = param.get("openIdent");
            String project_name = param.get("project_name");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Map<String, Object> ma = consumerService.getUserByUsername(String.valueOf(map.get("username")));
            List<Map<String, Object>> list = projectService.getSingleProject(String.valueOf(map.get("id")), s, n, project_name,ma.get("org_id"));
            modelMap.addAttribute("projectList", list);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 条件查询个人项目
     *
     * @param modelMap
     * @param s        开始序列
     * @param n        获取条数
     * @param param    openIdent及查询参数
     */
    @RequestMapping(value = "/searchProject", method = RequestMethod.GET)
    public void searchProject(ModelMap modelMap, @RequestParam Map<String, String> param, Integer s, Integer n) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Map<String, Object> ma = consumerService.getUserByUsername(String.valueOf(map.get("username")));

            List<Map<String, Object>> list = projectService.getProjectByParam(String.valueOf(map.get("id")), s, n, param,ma.get("org_id"));
            modelMap.addAttribute("projectList", list);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 条件查询个人所有项目
     *
     * @param modelMap
     * @param param    openIdent及查询参数
     */
    @RequestMapping(value = "/searchAllProject", method = RequestMethod.GET)
    public void searchAllProject(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Map<String, Object> ma = consumerService.getUserByUsername(String.valueOf(map.get("username")));

            List<Map<String, Object>> list = projectService.getAllProjectByParam(String.valueOf(map.get("id")), param,ma.get("org_id"));
            modelMap.addAttribute("projectList", list);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 创建项目
     *
     * @param modelMap
     * @param param    提交参数
     */
    @RequestMapping(value = "/createProject", method = RequestMethod.POST)
    public void createProject(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);

            Map<String, Object> map = consumerService.getUser(userEntity);
            //获取当前登陆人员的单位信息
            Map<String, Object> org = orgUserService.getOrgByUsername(userEntity.getA());
            if (org == null) {
                throw new CommonException("EXPO011");
            }
            param.put("design_unit", String.valueOf(org.get("id")));

            //组织机构
            Map<String,Object> partMap=consumerService.getTSpartByUserId(String.valueOf(map.get("id")));
            if(partMap!=null){
                param.put("cost_center", String.valueOf(partMap.get("org_id")));
                Map<String, Object> result = projectService.createProject(param, String.valueOf(map.get("id")));
                modelMap.addAllAttributes(result);
                modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
            }else{
                modelMap.addAllAttributes(SYS.PACK0012.msgMap());
            }


        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 删除项目阶段
     *
     * @param modelMap
     * @param param
     * @param stage_id 项目阶段id
     */
    @RequestMapping(value = "/delProjectStage", method = RequestMethod.POST)
    public void delProjectStage(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            projectService.delProjectStage(stage_id, String.valueOf(map.get("id")));
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 查询项目阶段信息
     *
     * @param modelMap
     * @param param
     * @param stage_id 阶段id
     */
    @RequestMapping(value = "/projectInfo", method = RequestMethod.GET)
    public void projectInfo(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id) {
        try {
            modelMap.addAllAttributes(projectService.projectInfo(stage_id));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改项目阶段基本信息
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/upProject", method = RequestMethod.POST)
    public void upProject(ModelMap modelMap, @RequestParam Map<String, Object> param) {
        try {
            projectService.upProject(param);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (ProjectRuntimeException e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).Msg);
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取数据权限类别
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/rabcClassify", method = RequestMethod.GET)
    public void RabcClassify(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("classify", projectService.rabcClassify(String.valueOf(map.get("id")), stage_id));
        } catch (ProjectRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).Msg);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取最后一个踏勘点（定位点）
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/getLastExp", method = RequestMethod.GET)
    public void getLastExp(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id, int classify,Integer pt_type) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);

            modelMap.addAttribute("lastExp", projectService.getLastExp(map.get("id"),stage_id, classify,pt_type));
        } catch (ProjectRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).Msg);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
